/*
 * Copyright 2006-2007 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.czeczek.domain.dao.impl;

import java.util.ArrayList;
import java.util.List;

import javax.sql.DataSource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
import org.springframework.jdbc.support.incrementer.DataFieldMaxValueIncrementer;

import com.czeczek.domain.IndicatorDim;
import com.czeczek.domain.IndicatorParamDim;
import com.czeczek.domain.dao.IndicatorsDao;
import com.czeczek.domain.dao.QuotesFactDao;
import com.czeczek.domain.dao.mapper.IndicatorsMapper;
import com.czeczek.domain.exceptions.IndicatorException;
import com.czeczek.domain.exceptions.IndicatorInstantiationException;
import com.czeczek.indicators.AbstractIndicator;
import com.czeczek.indicators.Indicator;

/**
 * Writes a Trade object to a database
 *
 * @author Marcin Czeczko
 */
public class JdbcIndicatorsDao implements IndicatorsDao {
	private Log log = LogFactory.getLog(JdbcIndicatorsDao.class);
    /**
     * template for inserting a row
     */
    private static final String SELECT_INDICATORS = "select i.id, i.indicator_name, i.indicator_classname, p.parameter_name, p.parameter_value from trader.indicator_dim i left outer join trader.indicator_param_dim p on (i.id=p.indicator_id)";

    /**
     * handles the processing of sql query
     */
    private SimpleJdbcTemplate simpleJdbcTemplate;

    /**
     * database is not expected to be setup for autoincrement
     */
    private DataFieldMaxValueIncrementer incrementer;

    /**
     * @see QuotesFactDao
     */
    public List<Indicator> getIndicators() throws Exception {    	
        List<Indicator> indicators = new ArrayList<Indicator>();
        
        log.debug("Reading indicators definitions");
        List<IndicatorDim> indicatorConfigs = simpleJdbcTemplate.query(SELECT_INDICATORS, new IndicatorsMapper());
        for(IndicatorDim item: indicatorConfigs) {
        	try {
				Class<? extends AbstractIndicator> clazz = Class.forName(item.getClassName()).asSubclass(AbstractIndicator.class);
				AbstractIndicator indicator = clazz.newInstance();
				for(IndicatorParamDim param: item.getParameters()) {
					indicator.setIndicatorId(item.getId());
					indicator.setParameter(param.getName(), param.getValue());
				}
				indicators.add(indicator);
				
			} catch (ClassNotFoundException e) {
				throw new IndicatorInstantiationException(e);
			} catch (InstantiationException e) {
				throw new IndicatorInstantiationException(e);
			} catch (IllegalAccessException e) {
				throw new IndicatorInstantiationException(e);
			}
        }
        
        return indicators;
    }

    public void setDataSource(DataSource dataSource) {
        this.simpleJdbcTemplate = new SimpleJdbcTemplate(dataSource);
    }

    public void setIncrementer(DataFieldMaxValueIncrementer incrementer) {
        this.incrementer = incrementer;
    }

}
